#include "ResourceManager.h"

CResourceManager::CResourceManager(DirectX *dx, LPCWSTR path)
{
	_directX = dx;
	/*filePath = L"coin.png";
	paddleSrc = L"paddle.bmp";
	brickSrc = L"Brick.png";
	marioMoveLeftSrc = L"Images\\MarioMoveLeft.png";
	marioMoveRightSrc = L"Images\\MarioMoveright.png";*/
	 _resourceConfigPath = path;

}

CResourceManager::~CResourceManager(void)
{

}

void CResourceManager::ReadLinkResource()
{
	ifstream inp("LinkResource.txt");
	int id;
	char* str = new char[100];
	while (inp)
	{
		inp>>id;
		inp>>str;
	}
	inp.close();
}

void CResourceManager::LoadImagePath()
{
	if (_numResource > 0)
	{
		_imagePathList = new char*[_numResource];
		ifstream inp("LinkResource.txt");
		int id = 0;
		char* str = new char[100];
		while (inp && id != -1) //id == -1: end of file
		{
			inp>>id;
			inp>>str;
			_imagePathList[id] = new char[strlen(str)];
			strcpy(_imagePathList[id],str);
		}
		inp.close();

	}
}

void CResourceManager::LoadConfig()
{
	ifstream inp(_resourceConfigPath);
	inp>>_numResource;

	if (_numResource == 0)
	{
		inp.close();
		return;
	}
	LoadImagePath();

	_spriteInfo = new int*[_numResource];
	for (int i=0;i<_numResource;i++)
	_spriteInfo[i] = new int[8];

	int j=0;
	while (inp && j!= -1)
	{
		inp>>j;
		inp>>_spriteInfo[j][WIDTH];
		inp>>_spriteInfo[j][HEIGHT];
		inp>>_spriteInfo[j][SPRITEPERROW];
		inp>>_spriteInfo[j][COUNT];
		inp>>_spriteInfo[j][ANIRATE];
	}
	inp.close();
	
//	if (_resourceConfigPath)
//	{
//		ifstream inp(_resourceConfigPath);
//		inp>>_numResource;
//
//
//		if (_numResource == 0)
//		{
//			inp.close();
//			return;
//		}
//
//		LoadImagePath();
//
//		//_spriteInfo = new int*[_numResource];
//		/*for (int i=0;i<_numResource;i++)
//			_spriteInfo[i] = new int[8];
//*/
//		int k;
//		while (inp)
//			inp>>k;
//		inp.close();
//	}
}

void CResourceManager::LoadResource()
{
	if (_numResource == 0) return;
	_textureList = new LPDIRECT3DTEXTURE9[_numResource];
	//_spriteList = new Sprite*[_numResource];
	char* s = new char[100];
	for (int i=0;i<_numResource;i++)
	{
		if (_imagePathList[i] != NULL)
		{
			s = _imagePathList[i];
			_textureList[i] = _directX->LoadTexture((LPCSTR)_imagePathList[i],D3DCOLOR_XRGB(0,0,0));
		}
		/*_spriteList[i] = new Sprite(getConstantName(i),_directX->getSpriteHandler(),_spriteInfo[i][WIDTH],_spriteInfo[i][HEIGHT],_spriteInfo[i][COUNT],_spriteInfo[i][SPRITEPERROW],_textureList[i],_spriteInfo[i][ANIRATE]);*/
	}
}

LPDIRECT3DTEXTURE9 CResourceManager::getTexture(int ResourceName)
{
	return _textureList[ResourceName];
}

Sprite* CResourceManager::getSprite(int ResourceName)
{
		Sprite* s = new Sprite(getConstantName(ResourceName),_directX->getSpriteHandler(),_spriteInfo[ResourceName][WIDTH],_spriteInfo[ResourceName][HEIGHT],_spriteInfo[ResourceName][COUNT],_spriteInfo[ResourceName][SPRITEPERROW],_textureList[ResourceName],_spriteInfo[ResourceName][ANIRATE]);
	return s;
}

char* CResourceManager::getConstantName(int constantName)
{
	char* result="";
	switch(constantName)
	{
		case MARIOMOVELEFT1: 
		result = "MARIOMOVELEFT1";
		break;
		case MARIOMOVERIGHT1: 
		result = "MARIOMOVERIGHT1";
		break;
		case MARIOSTANDLEFT1: 
		result = "MARIOSTANDLEFT1";
		break;
		case MARIOSTANDRIGHT1: 
		result = "MARIOSTANDRIGHT1";
		break;
		case MARIOJUMPLEFT1: 
		result = "MARIOJUMPLEFT1";
		break;
		case MARIOJUMPRIGHT1: 
		result = "MARIOJUMPRIGHT1";
		break;
		case BRICK:
			result ="BRICK";
			break;
		default:
			result = "";
			break;
	}

	return result;
}